package com.siecom.transport.handler;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.protobuf.ByteString;
import com.siecom.constconfig.ConstDefine;
import com.siecom.framework.bean.IdentityInfoBean;
import com.siecom.framework.device.SiecomDevice;
import com.siecom.framework.device.SiecomTask;
import com.siecom.hostservice.MainActivity;
import com.siecom.module.DeviceCallback;
import com.siecom.module.DeviceInterface;
import com.siecom.proto.Message;
import com.siecom.tool.IDPicture;
import com.siecom.tool.LogUtil;
import com.siecom.transport.CommServer;
import com.siecom.transport.bluetooth.BlueServer;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created on 2017/1/14.
 */

public class IdentityHandler implements ServiceHandler {
    private static final String TAG = IdentityHandler.class.getSimpleName();
    private CommServer server;
    @Override
    public void onReceive(CommServer server, Message.siecomproto msg,String message) {
        this.server = server;
        disposeMessage(msg,message);

    }

    public void disposeMessage(Message.siecomproto msg,String message){
        switch (msg.getCmd()){
            case ConstDefine.ID_READ_REQUEST:
                LogUtil.e(TAG,"进入读身份证");
                read(msg);
                break;
            case ConstDefine.IDENTITY_CANCEL_REQUEST:
                cancelId(msg);
                break;
        }

    }
    public void cancelId(Message.siecomproto msg){
        DeviceInterface.cancelIdentity();
        com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
        msgBuilder.setModule(msg.getModule());
        msgBuilder.setMsgid(msg.getMsgid());
        msgBuilder.setCmd(msg.getCmd() + 1);
        msgBuilder.setRespond("id cancel");
        msgBuilder.setError(0);
        com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
        server.sendToRemote(respondMsg);
        // 11键盘  10指纹 9IC卡  8磁条卡  5蓝牙 6 身份证
        MainActivity.getmSys().DLL_SysLedControl((byte) 6,(byte)0);

    }
    public void read(Message.siecomproto msg){
        //设置超时时间最少为10秒
        int timeOut = Math.max(msg.getTimeout(), 10 * 1000);
//        timeOut = 10*1000;  //超时时间设置为10秒
        SiecomDevice.ReadIdentity(false, new SiecomTask.TaskCallback() {
            @Override
            public void onStart(Bundle bundle) {
                // 11键盘  10指纹 9IC卡  8磁条卡  5蓝牙 6 身份证
                MainActivity.getmSys().DLL_SysLedControl((byte) 6,(byte)1);
                MainActivity.startIDAnime();
            }

            @Override
            public void onSucceed(Bundle bundle) {
                LogUtil.e("身份证","onSuccess 读取成功");
                IdentityInfoBean bean = bundle.getParcelable("data");
                Gson gson = new Gson();
                String respond = gson.toJson(bean);

                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid("");
                msgBuilder.setModule(ConstDefine.ID_MODULE);
                msgBuilder.setCmd(ConstDefine.ID_RESPOND);
//                msgBuilder.setRespond(respond);
                if(bean.imageByte != null){
                    msgBuilder.setExtion(ByteString.copyFrom(bean.imageByte));
                }
                msgBuilder.setError(0);
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
                MainActivity.playVoice(ConstDefine.OPERATE_SUCC);
                //停止动画
                MainActivity.stopIDAnime();
                // 11键盘  10指纹 9IC卡  8磁条卡  5蓝牙 6 身份证
                MainActivity.getmSys().DLL_SysLedControl((byte) 6,(byte)0);
            }

            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.ID_MODULE);
                msgBuilder.setCmd(ConstDefine.ID_RESPOND);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
                MainActivity.stopIDAnime();
                MainActivity.playVoice(ConstDefine.OPERATE_FAIL);
                // 11键盘  10指纹 9IC卡  8磁条卡  5蓝牙 6 身份证
                MainActivity.getmSys().DLL_SysLedControl((byte) 6,(byte)0);
            }
        },timeOut);


    }

}
